package com.ctbri.gui;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import com.ctbri.util.SwingConsole;

import java.awt.*;
import java.awt.event.*;

public class Ex16_List extends JFrame {
  private String[] flavors = {
    "Chocolate", "Strawberry", "Vanilla Fudge Swirl",
    "Mint Chip", "Mocha Almond Fudge", "Rum Raisin",
    "Praline Cream", "Mud Pie"
  };
  private JList lst = new JList(flavors);
  private JTextArea t =
    new JTextArea(flavors.length, 20);

  private ListSelectionListener ll =
    new ListSelectionListener() {
      public void valueChanged(ListSelectionEvent e) {
        if(e.getValueIsAdjusting()) return;
        t.setText("");
        for(Object item : lst.getSelectedValues())
          t.append(item + "\n");
      }
    };
  private int count = 0;
  public Ex16_List() {
    t.setEditable(false);
    setLayout(new FlowLayout());
    // Create Borders for components:
    Border brd = BorderFactory.createMatteBorder(
      1, 1, 2, 2, Color.BLACK);
    lst.setBorder(brd);
    t.setBorder(brd);
    // Add the first four items to the List
    add(t);
    add(lst);
    // Register event listeners
    lst.addListSelectionListener(ll);
  }
  public static void main(String[] args) {
    SwingConsole.run(new Ex16_List(), 250, 375);
  }
} ///:~
